<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2019-12-10
 * Time: 14:16
 */
namespace app\api\controller\v1;
use app\api\controller\BaseController;
use app\api\model\CityPartner;
use app\api\model\CountyPartner;
use app\api\model\ProvincePartner;
use app\api\model\Assistant as AssistantModel;
use app\api\model\UserInvitation;
use think\facade\Request;
use app\api\service\Token as TokenService;
use app\api\model\User as UserModel;

class User extends BaseController
{
    /**
     * 前置方法
     */
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'getMoneyRecord']
    ];

    /**
     * 更新用户信息并返回用户最新信息
     * @http put
     * @url user
     */
    public function updateUser()
    {
        $uid = TokenService::getCurrentUid();
        $put = Request::put();
        $user = new UserModel();
        $user->allowField(true)->save([
            'nickname' => $put['nickname'],
            'head_img' => $put['head_img'],
            'province' => $put['province'],
            'status' => 1,
            'city' => $put['city'],
            'gender' => $put['gender']
        ],['id' => $uid]);
        return json(UserModel::get($uid),202);
    }

    /**
     * 用户会员费记录
     */
    public function getMoneyRecord()
    {
        $uid = TokenService::getCurrentUid();
        return UserMoneyRecord::where('user_id','=',$uid)
            ->with('unuser')
            ->order('create_time desc')
            ->select();
    }
    /**
     * 获取用户信息
     * @http get
     * @url user
     */
    public function getUserInfo()
    {
        $uid = TokenService::getCurrentUid();
        $info =  UserModel::where('id',$uid)
            ->with(['member'=>['justImg','backImg']])
            ->find();
        if($info['partner_status'] > 0){
           if($info['partner_status'] == 1){
               $info['partner'] = ProvincePartner::where('user_id',$info['id'])->find();
           }elseif($info['partner_status'] == 2) {
               $info['partner'] = CityPartner::where('user_id',$info['id'])->find();
           }elseif($info['partner_status'] == 3) {
               $info['partner'] = CountyPartner::where('user_id',$info['id'])->find();
           }elseif($info['partner_status'] == 4) {
               $info['partner'] = AssistantModel::where('user_id',$info['id'])->find();
           }
        }
        return $info;
    }

    /**
     * 绑定上下级
     */
    public function createInvite()
    {
        $unuser_id = Request::post('unuser_id');
        $userInfo = UserModel::get($unuser_id);
        if($userInfo['status'] == 2)
        {
            $uid = TokenService::getCurrentUid();
            if($unuser_id != $uid)
            {
                if ($userInfo) {
                    $invite = UserInvitation::where('invited_person_id', '=', $uid)
                        ->find();
                    if (!$invite) {
                        UserInvitation::create([
                            'inviter_id' => $unuser_id,
                            'invited_person_id' => $uid
                        ]);
                    }
                }
            }
        }
    }




}